/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;


import java.io.File;
import plugin.LibsThread;
import plugin.PluginParalelThreadStart;

import system.ExitTimer;
import system.Logging;
import tasks.FMR;
import system.MessageOUTPUT;
import system.ReadSettings;
import system.UserInputStream;
import system.getTimeDate;
import tasks.TaskDo;
import tasks.getNextTask;

/**
 *
 * @author ZeM
 */
public class StartIDDS {
/**
 * Start of main part of the program
 */
public StartIDDS(){
new Parameters();
            Parameters.TotalTasks = -1;
            Parameters.TasksDone = 0;
            Parameters.exit = false;
            Parameters.ExitTimerIsWorking = false;
            Parameters.MainLogCreated=false;


            //reading settings file
            new ReadSettings();

            //create log file

            new Logging().CreateLogFile();
            new Logging().AddEvent("System srartup");
            new MessageOUTPUT().OUTPUT("s0000",null);
            //setting some parameters
            Parameters.TaskName = new String[Parameters.MaxTaskNumber];
            Parameters.taskAssignTime = new String[Parameters.MaxTaskNumber];
            if (Parameters.DeleteTaskWhenDone == 1 | (Parameters.DeleteTaskWhenDone == 0 && Parameters.MoveDoneTask == 1)) {
                Parameters.AdrOfDoneTasks = new String[Parameters.MaxTaskNumber];
            }


            //loading all autostart libs in threads
            for (int k=0; k<Parameters.AutostartLibs.length;k++){
                if(Parameters.AutostartLibs[k]!=null){
            LibsThread lt=new LibsThread();
            lt.lib_name=Parameters.AutostartLibs[k];
            new Thread(lt).start();
                }
            }

            

            //loading autostart plugins in threads
            for (int k=0; k<Parameters.AutoloadPlugins.length; k++){
                if (Parameters.AutoloadPlugins[k]!=null && Parameters.AutoloadPlugins[k].length()>0){
            PluginParalelThreadStart ppts=new PluginParalelThreadStart();
            ppts.PluginName=Parameters.AutoloadPlugins[k];
            new Thread(ppts).start();
                }
            }

            //loading Console input reader
            new Thread(new UserInputStream()).start();

            //main part:

            while (!Parameters.exit) { // if there is no need to exit
                //getting current system time
                Parameters.CurrentSystemTime = new getTimeDate().getTimeDate("yyyyMMddHHmmss");
                try {
                    if (Parameters.TotalTasks == 0) {

                        Thread.sleep(Parameters.FindTaskPause * 1000);
                    }
                } catch (InterruptedException ex) {
                    System.err.println(ex);
                }
                //scan folder for task files
                new FMR().Find();

                //checking if we have tasks to do
                if (Parameters.TotalTasks > 0) {
                     new Logging().AddEvent("Find " +Parameters.TotalTasks+ " tasks.");
                    //Printing information about how much tasks left to do
                    String[] mes = {"" + Parameters.TotalTasks};
                   new MessageOUTPUT().OUTPUT("ev0001",mes);
                    //getting current system time
                    Parameters.CurrentSystemTime = new getTimeDate().getTimeDate("yyyyMMddHHmmss");
                    //getting task adress
                    new getNextTask();

                    if (Parameters.CurrentTaskNumber > -1) {
                        if (Parameters.ExitTimerIsWorking) {
                            ExitTimer.stop = true;
                        }

                        String adress_of_new_task = "" + Parameters.TaskFolder + Parameters.TaskName[Parameters.CurrentTaskNumber];

                        //start doing task
                        Parameters.TaskFailed = false;
                        new Logging().AddEvent("Start doing task " +Parameters.TaskName[Parameters.CurrentTaskNumber]);
                        new TaskDo(adress_of_new_task);


                        if ((Parameters.DeleteTaskWhenDone == 1 & !Parameters.TaskFailed) | (Parameters.DeleteTaskWhenFail==1 & Parameters.TaskFailed)) {//delete task
                            new Logging().AddEvent("Deleting task " +Parameters.TaskName[Parameters.CurrentTaskNumber]);
                            new File("" + Parameters.TaskFolder + Parameters.TaskName[Parameters.CurrentTaskNumber]).delete();
                        } else if ((Parameters.MoveDoneTask == 1 && !Parameters.TaskFailed) |  (Parameters.MoveFailedTask == 1 && Parameters.TaskFailed)) {//move task when done or fail
                            new Logging().AddEvent("Moving task " +Parameters.TaskName[Parameters.CurrentTaskNumber]);
                            new FMR().Move();

                        } else if ((Parameters.MoveDoneTask == 2 && !Parameters.TaskFailed) |  (Parameters.MoveFailedTask == 2 && Parameters.TaskFailed)) {//rename task when done or fail
                            new Logging().AddEvent("Renaming task " +Parameters.TaskName[Parameters.CurrentTaskNumber]);
                            new FMR().Rename();

                        }


                        if (Parameters.TotalTasks > 1) {//if it's not the last task
                            Parameters.TaskName[Parameters.CurrentTaskNumber] = Parameters.TaskName[Parameters.TotalTasks - 1];
                            Parameters.taskAssignTime[Parameters.CurrentTaskNumber] = Parameters.taskAssignTime[Parameters.TotalTasks - 1];
                        }

                        if (Parameters.DeleteTaskWhenDone == 0 && ((Parameters.MoveDoneTask == 0 && !Parameters.TaskFailed) | ( Parameters.MoveFailedTask == 0 && Parameters.TaskFailed))) {
                            Parameters.AdrOfDoneTasks[Parameters.TasksDone] = adress_of_new_task;
                            Parameters.TasksDone++;
                        }

                    }



                } else if (Parameters.QuitTime != -1 && !Parameters.exit) { //if no more tasks left ant exit time >=0
                    if (!Parameters.ExitTimerIsWorking) {
                        new Logging().AddEvent("Start 'exit' timer");
                        //start timer
                        String[] mes = {"" + Parameters.QuitTime};
                        new MessageOUTPUT().OUTPUT("ev0002",mes);
                        Thread timer = new Thread(new ExitTimer());
                        timer.start();

                    }

                }
            }

}
}
